package com.sailfish.springbootdemo.service.db1;

import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.db1.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonJudgeCanDeleteServiceImpl implements CommonJudgeCanDeleteService {

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Autowired
    private BaseCapacityRuleDao baseCapacityRuleDao;

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private PcbaDeviceDao pcbaDeviceDao;

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private LineTechSmallTypeDao lineTechSmallTypeDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private PcbaDeviceParamTypeDao pcbaDeviceParamTypeDao;

    @Autowired
    private TechDeviceParamTypeDao techDeviceParamTypeDao;

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private BrandTypeDao brandTypeDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaLineDao pcbaLineDao;

    @Autowired
    private PcbaWorkshopDao pcbaWorkshopDao;

    @Autowired
    private RoadmapBigTypeDao roadmapBigTypeDao;

    @Autowired
    private RoadmapSmallTypeDao roadmapSmallTypeDao;

    @Autowired
    private RoadmapParamDao roadmapParamDao;

    /**
     * 检索所有的参与逻辑的key值，此类不允许删除，判断传入的key值在不在里面【基线】
     */
    @Override
    public Map<String, Integer> judgeKeyInLogic(String judgeKey) throws Exception {
        // 1.tb_tech_device_param 表中的 bind_device_param_key 字段，多个逗号区分的
        // 2.tb_base_capacity_rule 表中的 pcba_device_param_keys 字段，多个逗号区分的
        List<String> keyList1 = new ArrayList<>();
        List<String> keyList2 = new ArrayList<>();

        List<TechDeviceParam> techDeviceParamList = techDeviceParamDao.getAllTechDeviceParam(null, null, null);
        List<BaseCapacityRule> baseCapacityRuleList = baseCapacityRuleDao.getAllBaseCapacityRule(null, null, null);
        for (TechDeviceParam techDeviceParam : techDeviceParamList) {
            if (techDeviceParam.getBindDeviceParamKey() != null && !"".equals(techDeviceParam.getBindDeviceParamKey())) {
                String[] arr = techDeviceParam.getBindDeviceParamKey().split(",");
                for (String s : arr) {
                    keyList1.add(s);
                }
            }
        }
        for (BaseCapacityRule baseCapacityRule : baseCapacityRuleList) {
            if (baseCapacityRule.getPcbaDeviceParamKeys() != null && !"".equals(baseCapacityRule.getPcbaDeviceParamKeys())) {
                String[] arr = baseCapacityRule.getPcbaDeviceParamKeys().split(",");
                for (String s : arr) {
                    keyList2.add(s);
                }
            }
        }
        int relatedTechParamNum = 0;
        int relatedRuleNum = 0;
        for (String s : keyList1) {
            if (s.equals(judgeKey)) {
                relatedTechParamNum++;
            }
        }
        for (String s : keyList2) {
            if (s.equals(judgeKey)) {
                relatedRuleNum++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("relatedTechParamNum", relatedTechParamNum);
        map.put("relatedRuleNum", relatedRuleNum);
        return map;
    }

    /**
     * 判断基线资源能否删除
     *
     * @param pcbaDeviceResourceId
     * @return
     * @throws Exception
     */
    @Override
    public Integer judgeCanDeletePcbaDeviceResource(Integer pcbaDeviceResourceId) throws Exception {
        List<PcbaDevice> list = pcbaDeviceDao.getPcbaDeviceResourceByPcbaDeviceResourceId(pcbaDeviceResourceId);
        return list.size();
    }

    /**
     * 判断 基线整线能力中 项目大类 是否能被删除
     */
    public Map<String, Integer> judgeCanDeleteLineBaseBigType(Integer lineBaseBigTypeId) throws Exception {
        // 1.tb_line_base_small_type 表中的 line_base_big_type_id 字段
        // 2.base_capacity_rule 表中的 line_base_big_type_id 字段
        Map<String, Integer> map = new HashMap<>();
        List<LineBaseSmallType> allLineBaseSmallTypeByBigType = lineBaseSmallTypeDao.getAllLineBaseSmallTypeByBigType(null, lineBaseBigTypeId);
        map.put("lineBaseSmallTypeNum", allLineBaseSmallTypeByBigType.size());
        List<BaseCapacityRule> allBaseCapacityRule = baseCapacityRuleDao.getAllBaseCapacityRule(null, lineBaseBigTypeId, null);
        map.put("baseCapacityRuleNum", allBaseCapacityRule.size());
        return map;
    }

    /**
     * 判断 工艺整线能力中 项目大类 是否能被删除
     */
    public Map<String, Integer> judgeCanDeleteLineTechBigType(Integer lineTechBigTypeId) throws Exception {
        // 1.tb_line_tech_small_type 表中的 line_tech_big_type_id 字段
        // 2.tech_capacity_rule 表中的 line_tech_big_type_id 字段
        Map<String, Integer> map = new HashMap<>();
        List<LineTechSmallType> allLineTechSmallTypeByBigType = lineTechSmallTypeDao.getAllLineTechSmallTypeByBigType(null, lineTechBigTypeId);
        map.put("lineTechSmallTypeNum", allLineTechSmallTypeByBigType.size());
        List<TechCapacityRule> allTechCapacityRule = techCapacityRuleDao.getAllTechCapacityRule(null, lineTechBigTypeId, null);
        map.put("techCapacityRuleNum", allTechCapacityRule.size());
        return map;
    }

    /**
     * 判断 基线整线能力中 项目小类 是否能被删除
     */
    public Map<String, Integer> judgeCanDeleteLineBaseSmallType(Integer lineBaseSmallTypeId) throws Exception {
        // 1.base_capacity_rule 表中的 line_base_small_type_id 字段
        Map<String, Integer> map = new HashMap<>();
        List<BaseCapacityRule> allBaseCapacityRule = baseCapacityRuleDao.getAllBaseCapacityRule(null, null, lineBaseSmallTypeId);
        map.put("baseCapacityRuleNum", allBaseCapacityRule.size());
        return map;
    }

    /**
     * 判断 基线整线能力中 项目小类 是否能被删除
     */
    public Map<String, Integer> judgeCanDeleteLineTechSmallType(Integer lineTechSmallTypeId) throws Exception {
        // 1.tech_capacity_rule 表中的 line_tech_small_type_id 字段
        Map<String, Integer> map = new HashMap<>();
        List<TechCapacityRule> allTechCapacityRule = techCapacityRuleDao.getAllTechCapacityRule(null, null, lineTechSmallTypeId);
        map.put("techCapacityRuleNum", allTechCapacityRule.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaDeviceType(Integer pcbaDeviceTypeId) throws Exception {
        // 1.tb_pcba_device_param_type
        // 2.tb_tech_device_param_type
        // 3.tb_pcba_device_resource
        Map<String, Integer> map = new HashMap<>();
        List<PcbaDeviceResource> pcbaDeviceResourceList = pcbaDeviceResourceDao.getAllByDeviceTypeAndSearch("", pcbaDeviceTypeId, null, null, null, null, null, null, null, null, null, null);
        map.put("pcbaDeviceResourceNum", pcbaDeviceResourceList.size());
        List<PcbaDeviceParamType> pcbaDeviceParamTypeList = pcbaDeviceParamTypeDao.getPcbaDeviceParamTypeByPcbaDeviceTypeId(pcbaDeviceTypeId, "");
        map.put("pcbaDeviceParamTypeNum", pcbaDeviceParamTypeList.size());
        List<TechDeviceParamType> techDeviceParamTypeList = techDeviceParamTypeDao.getAllTechDeviceParamTypeByDeviceType(pcbaDeviceTypeId, "");
        map.put("techDeviceParamTypeNum", techDeviceParamTypeList.size());
        return map;
    }

    // 判断是否可以删除品牌
    @Override
    public Map<String, Integer> judgeCanDeleteBrand(Integer brandId) throws Exception {
        // 1.tb_brand_type
        // 2.tb_pcba_device_resource
        Map<String, Integer> map = new HashMap<>();
        List<BrandType> brandTypeList = brandTypeDao.getAllBrandType(null, brandId);
        map.put("brandTypeNum", brandTypeList.size());
        List<PcbaDeviceResource> pcbaDeviceResourceList = pcbaDeviceResourceDao.getAllByBrandOrBrandType(brandId, null);
        map.put("pcbaDeviceResourceNum", pcbaDeviceResourceList.size());
        return map;
    }

    // 判断是否可以删除型号
    @Override
    public Map<String, Integer> judgeCanDeleteBrandType(Integer brandTypeId) throws Exception {
        // 1.tb_pcba_device_resource
        Map<String, Integer> map = new HashMap<>();
        List<PcbaDeviceResource> pcbaDeviceResourceList = pcbaDeviceResourceDao.getAllByBrandOrBrandType(null, brandTypeId);
        map.put("pcbaDeviceResourceNum", pcbaDeviceResourceList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaDeviceParamType(Integer pcbaDeviceParamTypeId) throws Exception {
        // 1.tb_pcba_device_param
        Map<String, Integer> map = new HashMap<>();
        List<PcbaDeviceParam> pcbaDeviceParamList = pcbaDeviceParamDao.getByPcbaDeviceParamTypeId(pcbaDeviceParamTypeId);
        map.put("pcbaDeviceParamNum", pcbaDeviceParamList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeleteTechDeviceParamType(Integer techDeviceParamTypeId) throws Exception {
        // 1.tb_tech_device_param
        Map<String, Integer> map = new HashMap<>();
        List<TechDeviceParam> techDeviceParamList = techDeviceParamDao.getByTechDeviceParamTypeId(techDeviceParamTypeId);
        map.put("techDeviceParamNum", techDeviceParamList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaProcess(Integer pcbaProcessId) throws Exception {
        // 1.tb_pcba_line
        Map<String, Integer> map = new HashMap<>();
        List<PcbaLine> pcbaLineList = pcbaLineDao.getPcbaLineByCondition(null, null, pcbaProcessId);
        map.put("pcbaLineNum", pcbaLineList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaCompany(Integer pcbaCompanyId) throws Exception {
        // 1.tb_pcba_line
        // 2.tb_pcba_workshop
        Map<String, Integer> map = new HashMap<>();
        List<PcbaLine> pcbaLineList = pcbaLineDao.getPcbaLineByCondition(pcbaCompanyId, null, null);
        List<PcbaWorkshop> pcbaWorkshopList = pcbaWorkshopDao.getAllPcbaWorkshopByCompanyId(null, pcbaCompanyId);
        map.put("pcbaLineNum", pcbaLineList.size());
        map.put("pcbaWorkshopNum", pcbaWorkshopList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaWorkshop(Integer pcbaWorkshopId) throws Exception {
        // 1.tb_pcba_line
        Map<String, Integer> map = new HashMap<>();
        List<PcbaLine> pcbaLineList = pcbaLineDao.getPcbaLineByCondition(null, pcbaWorkshopId, null);
        map.put("pcbaLineNum", pcbaLineList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeleteRoadmapBigType(Integer roadmapBigTypeId) throws Exception {
        // 1.tb_roadmap_small_type
        // 2.tb_roadmap_param
        Map<String, Integer> map = new HashMap<>();
        List<RoadmapSmallType> roadmapSmallTypeList = roadmapSmallTypeDao.getAllRoadmapSmallType(null, roadmapBigTypeId);
        List<RoadmapParam> roadmapParamList = roadmapParamDao.getAllRoadmapParam(null, roadmapBigTypeId, null);
        map.put("roadmapSmallTypeNum", roadmapSmallTypeList.size());
        map.put("roadmapParamNum", roadmapParamList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeleteRoadmapSmallType(Integer roadmapSmallTypeId) throws Exception {
        // 1.tb_roadmap_param
        Map<String, Integer> map = new HashMap<>();
        List<RoadmapParam> roadmapParamList = roadmapParamDao.getAllRoadmapParam(null, null, roadmapSmallTypeId);
        map.put("roadmapParamNum", roadmapParamList.size());
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeletePcbaDeviceParam(String pcbaDeviceParamKey) throws Exception {
        // 1.tb_tech_device_param 表中的 bind_device_param_key 字段，多个逗号区分的
        // 2.tb_base_capacity_rule 表中的 pcba_device_param_keys 字段，多个逗号区分的
        List<String> keyList1 = new ArrayList<>();
        List<String> keyList2 = new ArrayList<>();

        List<TechDeviceParam> techDeviceParamList = techDeviceParamDao.getAllTechDeviceParam(null, null, null);
        List<BaseCapacityRule> baseCapacityRuleList = baseCapacityRuleDao.getAllBaseCapacityRule(null, null, null);
        for (TechDeviceParam techDeviceParam : techDeviceParamList) {
            if (techDeviceParam.getBindDeviceParamKey() != null && !"".equals(techDeviceParam.getBindDeviceParamKey())) {
                String[] arr = techDeviceParam.getBindDeviceParamKey().split(",");
                for (String s : arr) {
                    keyList1.add(s);
                }
            }
        }
        for (BaseCapacityRule baseCapacityRule : baseCapacityRuleList) {
            if (baseCapacityRule.getPcbaDeviceParamKeys() != null && !"".equals(baseCapacityRule.getPcbaDeviceParamKeys())) {
                String[] arr = baseCapacityRule.getPcbaDeviceParamKeys().split(",");
                for (String s : arr) {
                    keyList2.add(s);
                }
            }
        }
        int relatedTechParamNum = 0;
        int relatedRuleNum = 0;
        for (String s : keyList1) {
            if (s.equals(pcbaDeviceParamKey)) {
                relatedTechParamNum++;
            }
        }
        for (String s : keyList2) {
            if (s.equals(pcbaDeviceParamKey)) {
                relatedRuleNum++;
            }
        }
        Map<String, Integer> map = new HashMap<>();
        map.put("relatedTechParamNum", relatedTechParamNum);
        map.put("relatedRuleNum", relatedRuleNum);
        return map;
    }

    @Override
    public Map<String, Integer> judgeCanDeleteTechDeviceParam(String techDeviceParamKey) throws Exception {
        // 1.tb_roadmap_param 表中的 tech_device_param_keys 字段，多个逗号区分的
        List<String> keyList1 = new ArrayList<>();

        List<RoadmapParam> roadmapParamList = roadmapParamDao.getAllRoadmapParam(null, null, null);
        for (RoadmapParam roadmapParam : roadmapParamList) {
            if (roadmapParam.getTechDeviceParamKeys() != null && !"".equals(roadmapParam.getTechDeviceParamKeys())) {
                String[] arr = roadmapParam.getTechDeviceParamKeys().split(",");
                for (String s : arr) {
                    keyList1.add(s);
                }
            }
        }
        int relatedRoadmapParamNum = 0;
        for (String s : keyList1) {
            if (s.equals(techDeviceParamKey)) {
                relatedRoadmapParamNum++;
            }
        }

        Map<String, Integer> map = new HashMap<>();
        map.put("relatedRoadmapParamNum", relatedRoadmapParamNum);
        return map;
    }
}
